package patternmining.model.construction;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.antlr.runtime.ANTLRInputStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.CommonTreeNodeStream;

/**
 * @version 1.0, 21.05.2008
 * @author Holger Wiehen
 */
public class BPSLSourceFile extends Source {
    private File source;
    
    public BPSLSourceFile(File file) throws WrongFileTypeException, IOException{
        if ( file.isFile() && Source.isBPSLDefinitionFile(file) )
            source = file;
        else
            throw new WrongFileTypeException("File " + file.getCanonicalPath() + " is not a BPSLDefinitionFile");
    }
    
    public ModelData modelData(){
        ModelData data = null;
        FileInputStream input = null;
        try{
            input = new FileInputStream(source);
            ANTLRInputStream antlrInput = new ANTLRInputStream(input);
            BPSLLexer lexer = new BPSLLexer(antlrInput);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            BPSLParser parser = new BPSLParser( tokens );
            CommonTree tree = (CommonTree) parser.specification().getTree();
            CommonTreeNodeStream treeAsNodeStream = new CommonTreeNodeStream(tree);
            BPSLASTSinglePass treewalker = new BPSLASTSinglePass(treeAsNodeStream);
            
            data = treewalker.specification();    
        }
        catch(IOException e){
            e.printStackTrace();
        }
        catch(RecognitionException e){
            e.printStackTrace();
        }
        finally{
            try{
                input.close();
            }
            catch(IOException e){
                
            }
        }
     return data;
    }
}
